package com.example.q_kang.hhh.login;


import android.animation.ValueAnimator;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.TextUtils;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.q_kang.hhh.R;
import com.example.q_kang.hhh.main.MainActivity;
import com.example.q_kang.hhh.util.CodeUtils;
import com.hjq.toast.ToastUtils;
import com.sskj.base.MVPActivity;
import com.sskj.base.api.SpConfig;
import com.sskj.base.base.App;
import com.sskj.base.utils.CheckUtil;
import com.sskj.base.utils.ClickUtil;
import com.sskj.base.utils.SPUtils;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author Hey
 * Create at  2019/04/11
 */
public class LoginActivity extends MVPActivity<LoginPresenter> {
    @BindView(R.id.rl_see)
    CheckBox rlSee;
    @BindView(R.id.iv_picture_code)
    ImageView ivPictureCode;
    @BindView(R.id.et_mobile)
    EditText etMobile;
    @BindView(R.id.et_pwd)
    EditText etPwd;
    @BindView(R.id.et_picture_code)
    EditText etPictureCode;
    @BindView(R.id.tv_forget_pwd)
    TextView tvForgetPwd;
    @BindView(R.id.btn_login)
    Button btnLogin;
    @BindView(R.id.rl_register)
    LinearLayout rlRegister;
    @BindView(R.id.iv_splash)
    ImageView ivSplash;
    private CodeUtils codeUtils;
    private Bitmap bitmap;

    @Override
    public int getLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    public LoginPresenter getPresenter() {
        return new LoginPresenter();
    }

    @Override
    public void initView() {
        if (SPUtils.get(App.INSTANCE, SpConfig.LOGIN, "").equals("1")) {
            loginSuccess();
        }
        String show = getIntent().getStringExtra("show");
        if (show != null) {
            ivSplash.setVisibility(View.GONE);
        } else {
            ivSplash.setVisibility(View.VISIBLE);
            hideImage();
        }
        codeUtils = CodeUtils.getInstance();
        bitmap = codeUtils.createBitmap();
        ivPictureCode.setImageBitmap(bitmap);
        ClickUtil.click(300, ivPictureCode, view -> {
            bitmap = codeUtils.createBitmap();
            ivPictureCode.setImageBitmap(bitmap);
        });
        rlSee.setOnCheckedChangeListener((compoundButton, b) -> {
            if (!b) {
                etPwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
            } else {
                etPwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
            }
        });
        ClickUtil.click(300, tvForgetPwd, view -> ForgetPwdActivity.start(this));
        ClickUtil.click(300, rlRegister, view -> RegisterActivity.start(this));
        ClickUtil.click(300, btnLogin, new ClickUtil.Click() {
            @Override
            public void click(View view) {
                if (TextUtils.isEmpty(etMobile.getText())) {
                    ToastUtils.show(App.INSTANCE.getString(R.string.appJavaLoginActivity2));
                    return;
                }
                if (!CheckUtil.isMobile(etMobile.getText().toString())) {
                    ToastUtils.show(App.INSTANCE.getString(R.string.appJavaLoginActivity3));
                    return;
                }
                if (TextUtils.isEmpty(etPwd.getText())) {
                    ToastUtils.show(App.INSTANCE.getString(R.string.appJavaLoginActivity4));
                    return;
                }
                if (TextUtils.isEmpty(etPictureCode.getText().toString())) {
                    ToastUtils.show(App.INSTANCE.getString(R.string.appJavaLoginActivity5));
                    return;
                }
                String code = codeUtils.getCode();
                if (!code.equalsIgnoreCase(etPictureCode.getText().toString())) {
                    ToastUtils.show(App.INSTANCE.getString(R.string.appJavaLoginActivity6));
                    bitmap = codeUtils.createBitmap();
                    ivPictureCode.setImageBitmap(bitmap);
                    return;
                }
                mPresenter.login(etMobile.getText().toString(),
                        etPwd.getText().toString());
            }
        });
    }

    @Override
    public void initData() {

    }

    private void hideImage() {
        ValueAnimator animator = ValueAnimator.ofFloat(1.0f, 0f).setDuration(3000);
        animator.addUpdateListener(animation -> {
            float value = (float) animation.getAnimatedValue();
            ivSplash.setAlpha(value);
            if (value == 0f) {
                ivSplash.setVisibility(View.GONE);
            }
        });
        animator.start();
    }

    public static void start(Context context) {
        Intent intent = new Intent(context, LoginActivity.class);
        intent.putExtra("isShow", "1");
        context.startActivity(intent);
    }

    public void loginSuccess() {
        MainActivity.start(this);
        finish();
    }
}
